using System;
using System.Data;
using System.Globalization;
using System.Web.UI.WebControls;
using mojoPortal.Business;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using Resources;
using ud_Forum.Business;

namespace ud_Forum
{
	public partial class ForumModule : SiteModuleControl
	{
	    //protected string EditContentImage = WebConfigSettings.EditContentImage;
        //protected string RssImageFile = WebConfigSettings.RSSImageFileNameg;
		private SiteUser siteUser;
        protected bool EnableRSSAtModuleLevel;
        protected bool EnableRSSAtForumLevel;
        protected bool EnableRSSAtThreadLevel;
        protected bool showSubscriberCount;
        protected Double TimeOffset;
        private TimeZoneInfo timeZone;
        private int userId = -1;
        protected string notificationUrl = string.Empty;
        protected string notificationLink = string.Empty;
        private string instanceCssClass = string.Empty;
        private bool showForumSearchBox = true;


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
        }
        
        
        protected void Page_Load(object sender, EventArgs e)
		{
			
            
			Title1.EditUrl = SiteRoot + "/Forums/EditForum.aspx";
            Title1.EditText = ForumResources.EditImageAltText;

            Title1.Visible = !RenderInWebPartMode;
            if (ModuleConfiguration != null)
            {
                Title = ModuleConfiguration.ModuleTitle;
                Description = ModuleConfiguration.FeatureName;
            }
            LoadSettings();
            SetupCss();
			
			PopulateLabels();
			PopulateControls();
		}


		private void PopulateControls()
		{
            using (IDataReader reader = Forum.GetForums(ModuleId, userId))
            {
                rptForums.DataSource = reader;
#if MONO
			rptForums.DataBind();
#else
                DataBind();
#endif
            }

            if (rptForums.Items.Count == 0)
            {
                pnlForumList.Visible = false;
                searchBox1.Visible = false;
            }
         
		}


        protected string FormatSubscriberCount(int subscriberCount)
        {
            return string.Format(CultureInfo.InvariantCulture, ForumResources.SubscriberCountFormat, subscriberCount);

        }

        protected string FormatDate(object o)
        {
            if ((o == null)||(o == DBNull.Value)) { return string.Empty; }

            DateTime startDate = Convert.ToDateTime(o);

            if (timeZone != null)
            {
                return TimeZoneInfo.ConvertTimeFromUtc(startDate, timeZone).ToString();

            }

            return startDate.AddHours(TimeOffset).ToString();

        }
        

		private void PopulateLabels()
		{
            Title1.EditText = ForumResources.ForumEditLabel;
            //EditAltText = Resource.EditImageAltText;
            
            divEditSubscriptions.Visible = tdSubscribedHead.Visible = Page.Request.IsAuthenticated;
            notificationUrl = SiteRoot + "/Forums/EditSubscriptions.aspx?mid="
                + ModuleId.ToString(CultureInfo.InvariantCulture)
                + "&pageid=" + PageId.ToString(CultureInfo.InvariantCulture);

            editSubscriptionsLink.NavigateUrl = notificationUrl;

            notificationLink = "<a title='" + ForumResources.ForumModuleEditSubscriptionsLabel 
                + "' href='" + SiteRoot + "/Forums/EditSubscriptions.aspx?mid="
                + ModuleId.ToString(CultureInfo.InvariantCulture)
                + "&amp;pageid=" + PageId.ToString(CultureInfo.InvariantCulture)
                + "'><img src='" + ImageSiteRoot + "/Data/SiteImages/FeatureIcons/email.png' /></a>";

            lnkModuleRSS.NavigateUrl = SiteRoot
                + "/Forums/RSS.aspx?mid=" + ModuleId
                + "&pageid=" + currentPage.PageId;

            //lnkModuleRSS.ImageUrl = ImageSiteRoot + "/Data/SiteImages/" + RssImageFile;

            //lnkModuleRSS.Text = "RSS";

            lnkModuleRSS.Visible = EnableRSSAtModuleLevel;

            editSubscriptionsLink.Text = ForumResources.ForumModuleEditSubscriptionsLabel;

            

		}

        private void SetupCss()
        {
            // older skins have this
            if (Page.Master != null)
            {
                StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
                if (stylesheet != null)
                {
                    if (stylesheet.FindControl("forumcss") == null)
                    {
                        Literal cssLink = new Literal
                                              {
                                                  ID = "forumcss",
                                                  Text = @"<link href='"
                                                         + SiteUtils.GetSkinBaseUrl()
                                                         +
                                                         @"forummodule.css' type='text/css' rel='stylesheet' media='screen' />"
                                              };

                        stylesheet.Controls.Add(cssLink);
                    }
                }
            }
        }


        private void LoadSettings()
        {
            EnableRSSAtModuleLevel = WebUtils.ParseBoolFromHashtable(
                Settings, "ForumEnableRSSAtModuleLevel", false);

            EnableRSSAtForumLevel = WebUtils.ParseBoolFromHashtable(
                Settings, "ForumEnableRSSAtForumLevel", false);

            EnableRSSAtThreadLevel = WebUtils.ParseBoolFromHashtable(
                Settings, "ForumEnableRSSAtThreadLevel", false);

            showSubscriberCount = WebUtils.ParseBoolFromHashtable(
                Settings, "ShowSubscriberCount", showSubscriberCount);

            showForumSearchBox = WebUtils.ParseBoolFromHashtable(
                Settings, "ShowForumSearchBox", showForumSearchBox);

            if (!showForumSearchBox) { searchBox1.Visible = false; }

            if (Settings.Contains("CustomCssClassSetting"))
            {
                instanceCssClass = Settings["CustomCssClassSetting"].ToString();
            }
            if (instanceCssClass.Length > 0) { pnlWrapper.CssClass += " " + instanceCssClass; }

            //siteUser = new SiteUser(siteSettings, Context.User.Identity.Name);
            siteUser = SiteUtils.GetCurrentSiteUser();

            if (siteUser != null) userId = siteUser.UserId;

            
            TimeOffset = SiteUtils.GetUserTimeOffset();
            timeZone = SiteUtils.GetUserTimeZone();

            if (Page is mojoBasePage)
            {
                mojoBasePage basePage = Page as mojoBasePage;
                if (basePage.AnalyticsSection.Length == 0)
                {
                    basePage.AnalyticsSection = ConfigHelper.GetStringProperty("AnalyticsForumSection", "forums");
                }
            }
        }

    }
}
